package com.yand.thinkingInJava.thinkingInJava21.daemon;


import java.util.concurrent.TimeUnit;

class Daemon implements Runnable{

    private Thread [] t = new Thread[10];



    @Override
    public void run() {
       for(int i = 0 ; i < t.length; i ++){
           t[i] = new Thread(new DaemonSpawn());
           t[i].start();
           System.out.println("DaemonSpawn"+i+" started.");
       }

       for(int i = 0 ; i < t.length; i ++){
           System.out.println("t["+i+"].isDaemon = "+t[i].isDaemon());
       }


    }
}

class DaemonSpawn implements Runnable{

    @Override
    public void run() {
        while(true){
            Thread.yield();
        }
    }
}


public class Daemons {

    public static void main(String [] args){
        Thread d = new Thread(new Daemon());
        //一旦线程被设置成守护线程，它start中的其他初始化线程也被设置成守护线程
        d.setDaemon(true);
        d.start();
        try {
           // TimeUnit.MILLISECONDS.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
